#include <stdio.h>
#include <stdlib.h>

#define SWAP_INT(a, b)		(((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b)))

/* {{{ oops
int
fact(int n)
{
	register int i = 1, ret = 1;

	while (i <= n) {
		ret *= i;
		i++;
	}

	return ret;
}

int
choose(n, k)
{
	return fact(n) / (fact(k) * fact(n - k));
}
 }}} */

int
print_array_nl(const int * const a, const int length)
{
	int i;

	if (length < 0)
		return -1;

	for (i = 0; i < length; i++)
		printf("%d%s", a[i], i + 1 == length ? "" : " ");
	fputs("\n", stdout);

	return 0;
}

int
print_permutations_rec(int * const a, const int n, const int s)
{
	int i = 0, pos = 0;
	int *pa = a + s;

	if (s == n) {
		print_array_nl(a, n);
		return 1;
	}

	while (pos < n - s) {
		SWAP_INT(pa[0], pa[pos]);

		print_permutations_rec(a, n, s + 1);

		SWAP_INT(pa[pos], pa[0]);
		pos++;
	}

	return 0;
}

int
main(int argc, char **argv)
{
	static int n, a[8];
	int i;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);

	print_permutations_rec(a, n, 0);

	return 0;
}
// vim:foldmethod=marker:
